package com.topera.statisticservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topera.commonutils.R;
import com.topera.statisticservice.client.UcenterClient;
import com.topera.statisticservice.entity.StatisticsDaily;
import com.topera.statisticservice.mapper.StatisticsDailyMapper;
import com.topera.statisticservice.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-06-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        //删除已存在的统计对象
        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.eq("date_calculated", day);
        baseMapper.delete(dayQueryWrapper);

        R r = ucenterClient.countRegister(day);
        int registerNum = (int) r.getData().get("registerNum");

        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        //将获取的数据添加到统计分析表中

        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        baseMapper.insert(daily);

    }

    @Override
    public Map<String, Object> showData(String type, String begin, String end) {

        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);

        //返回两部分数据  日期 和 数值
        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for(StatisticsDaily item : statisticsDailies){
            dateList.add(item.getDateCalculated());
            switch (type){
                case "login_num" :
                    numList.add(item.getLoginNum());
                    break;

                case "register_num" :
                    numList.add(item.getRegisterNum());
                    break;
                case "video_view_num" :
                    numList.add(item.getVideoViewNum());
                    break;

                case "course_num":
                    numList.add(item.getCourseNum());
                    break;

                default:
                        break;
            }

        }

        Map<String,Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numList",numList);
        return map;
    }
}
